package cn.gavinnie.util;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.beanutils.BeanUtils;

import cn.gavinnie.dto.MimeMessageDTO;

/**
 * 类名称:  mailUtil
 * 功能描述: TODO 邮件发送例子
 * 创建人:  hh 
 * 创建时间: 2015-4-13 
 */
public class MailUtil {
	
	/**   
	 * 变量名 userName: TODO 邮箱用户名
	 */   
	private String userName;
	
	/**   
	 * 变量名 password: TODO 邮箱地址
	 */   
	private String password;
	
	/**
	 * 变量名 displayName: TODO 发件人显示名
	 */
	private String displayName;
	
	
	/**   
	 * 变量名 smtpHost: TODO 邮箱smtp地址，发送地址
	 */   
	private String smtpHost;
	
	/**   
	 * 变量名 targetAddress: TODO 收件邮箱地址
	 */   
	private String toAddress;
	
	/**
	 * 变量名 ccAddress: TODO 抄送邮箱地址
	 */
	private String ccAddress;
	
	/**
	 * 变量名 bccAddress: TODO 暗送邮箱地址
	 */
	private String bccAddress;
	/**
	 * 方法名: sendEmail 
	 * 功能描述: TODO 发送邮件
	 * @param userName	邮箱账号
	 * @param password	邮箱密码
	 * @param displayName	发件人显示名
	 * @param toAddress	收件邮箱地址
	 * @param ccAddress	抄送邮箱地址
	 * @param bccAddress	暗送邮箱地址	 
	 * @param mimeDTO 邮件内容
	 * @return
	 */
	public static boolean sendEmail(String userName,String password,String displayName,String toAddress,String ccAddress,String bccAddress,MimeMessageDTO mimeDTO){
		Properties props = new Properties();
		String host = SMTPUtil.SimpleMailSender(userName);
		props.put("mail.smtp.host", host);
		props.setProperty("mail.smtp.auth", "true");
		props.put("mail.smtp.user", userName);
		props.put("mail.smtp.password", password);
		Session session = Session.getInstance(props, new PopupAuthenticator(userName, password));
		if(displayName == null || "".equals(displayName)){
			displayName = userName.split("@")[0];
		}
		try {
			MimeMessage msg = new MimeMessage(session);
			//发件人
			msg.setFrom(new InternetAddress(userName,displayName));
			//收件人
			msg.setRecipients(Message.RecipientType.TO, toAddress);
			//抄送
			msg.setRecipients(Message.RecipientType.CC, ccAddress);
			//暗送
			msg.setRecipients(Message.RecipientType.BCC, bccAddress);
			//把DTO设置的内容，复制到msg中
			BeanUtils.copyProperties(msg, mimeDTO);
			//发送邮件
//			Transport.send(msg);
			
			//连接邮件服务器后要发送多个邮件时，下面这种方式比较高效
			msg.saveChanges(); // implicit with send()
			Transport transport = session.getTransport("smtp");
			transport.connect(host, userName, password);
			transport.sendMessage(msg, msg.getAllRecipients());
			transport.close();
			
		} catch (Exception mex) {
			mex.printStackTrace();
			return false;
		}
		return true;
	}
	/**
	 * 
	 * @param userName 发件人用户名
	 * @param password 密码
	 * @param toAddress 收件人邮件地址
	 * @param mimeDTO 邮件内容
	 * @return
	 */
	public static boolean sendEmail(String userName,String password,String toAddress,MimeMessageDTO mimeDTO){
		return sendEmail(userName,password,null,toAddress,null,null,mimeDTO);
	}
	/**
	 * 
	 * @param userName 发件人用户名
	 * @param password 密码
	 * @param displayName 发件人显示名
	 * @param toAddress 收件人邮箱地址
	 * @param mimeDTO 邮件内容
	 * @return
	 */
	public static boolean sendEmail(String userName,String password,String displayName,String toAddress,MimeMessageDTO mimeDTO){
		return sendEmail(userName,password,displayName,toAddress,null,null,mimeDTO);
	}
	//邮箱收件功能，目前没有调试成功，留此代码供以后参考
//	public static boolean receiveEmail(String userName,String password){
//		Properties props = new Properties();
//		props.put("mail.smtp.host", "pop3");
//		props.setProperty("mail.smtp.auth", "true");
//		props.put("mail.smtp.user", userName);
//		props.put("mail.smtp.password", password);
//		Session session = Session.getInstance(props, new PopupAuthenticator(userName, password));
//		try {
//			Store store = session.getStore("pop3");
//		} catch (NoSuchProviderException e) {
//			e.printStackTrace();
//		}
//		return false;
//	}

	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getSmtpHost() {
		return smtpHost;
	}
	public void setSmtpHost(String smtpHost) {
		this.smtpHost = smtpHost;
	}
	
	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public String getToAddress() {
		return toAddress;
	}

	public void setToAddress(String toAddress) {
		this.toAddress = toAddress;
	}

	public String getCcAddress() {
		return ccAddress;
	}

	public void setCcAddress(String ccAddress) {
		this.ccAddress = ccAddress;
	}

	public String getBccAddress() {
		return bccAddress;
	}

	public void setBccAddress(String bccAddress) {
		this.bccAddress = bccAddress;
	}
}
